package com.yanlin.apidriver.service;

import com.yanlin.internalcommon.dto.ResponseResult;
import com.yanlin.internalcommon.dto.TokenResult;
import com.yanlin.internalcommon.response.TokenResponse;
import com.yanlin.internalcommon.util.JwtUtils;
import com.yanlin.internalcommon.util.RedisPrefixUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import static com.yanlin.internalcommon.constant.CommonStatusEnum.TOKEN_ERROR;
import static com.yanlin.internalcommon.constant.TokenConstant.ACCESS_TOKEN_TYPE;
import static com.yanlin.internalcommon.constant.TokenConstant.REFRESH_TOKEN_TYPE;
import static com.yanlin.internalcommon.util.RedisPrefixUtil.ACCESS_TOKEN_EXPIRATINO;
import static com.yanlin.internalcommon.util.RedisPrefixUtil.REFRESH_TOKEN_EXPIRATINO;
import static java.util.concurrent.TimeUnit.DAYS;

/**
 * <p>标题：TokenService</p>
 * <p>功能：token业务类</p>
 * <p>
 * 其他说明：
 * </p>
 * <p>审核：</p>
 * <p>重构：</p>
 * <p>创建日期：2025-06-08 15:20</p>
 *
 * @author yanlin
 */
@Service
public class TokenService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public ResponseResult refreshToken(String refreshTokenSrc) {

        // 解析refreshToken
        TokenResult tokenResult = JwtUtils.checkToken(refreshTokenSrc);
        if (tokenResult == null) {
            return ResponseResult.fail(TOKEN_ERROR.getCode(), TOKEN_ERROR.getValue());
        }
        String phone = tokenResult.getPhone();
        String identity = tokenResult.getIdentity();
        String accessTokenKey = RedisPrefixUtil.getTokenKey(phone, identity, ACCESS_TOKEN_TYPE);
        String refreshTokenKey = RedisPrefixUtil.getTokenKey(phone, identity, REFRESH_TOKEN_TYPE);
        // 读取redis中的refreshToken
        String redisRefreshToken = stringRedisTemplate.opsForValue().get(refreshTokenKey);
        // 校验refreshToken
        if (StringUtils.isBlank(redisRefreshToken) || !redisRefreshToken.equals(refreshTokenSrc)) {
            return ResponseResult.fail(TOKEN_ERROR.getCode(), TOKEN_ERROR.getValue());
        }
        // 生成双token
        String accessTokenNew = JwtUtils.generateToken(phone, identity, ACCESS_TOKEN_TYPE);
        String refreshTokenNew = JwtUtils.generateToken(phone, identity, REFRESH_TOKEN_TYPE);
        stringRedisTemplate.opsForValue().set(accessTokenKey, accessTokenNew, ACCESS_TOKEN_EXPIRATINO, DAYS);
        stringRedisTemplate.opsForValue().set(refreshTokenKey, refreshTokenNew, REFRESH_TOKEN_EXPIRATINO, DAYS);
        /*stringRedisTemplate.opsForValue().set(accessTokenKey, accessTokenNew, 30, TimeUnit.SECONDS);
        stringRedisTemplate.opsForValue().set(refreshTokenKey, refreshTokenNew, 120, TimeUnit.SECONDS);*/

        TokenResponse tokenResponse = new TokenResponse();
        tokenResponse.setAccessToken(accessTokenNew);
        tokenResponse.setRefreshToken(refreshTokenNew);
        return ResponseResult.success(tokenResponse);
    }
}